<?php

use console\db\Migration;

/**
 * 创建应用相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091330_t_app extends Migration {

    /**
     * APP客户端表
     * @table `app_client`
     */
    public function buildTableAppClient() {
        return [
            'comment' => 'APP客户端表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->char(32)->notNull()->comment('应用ID'),
                'secret' => $this->string(32)->notNull()->comment('密钥'),
                'descirption' => $this->string(255)->comment('备注'),
                'type' => $this->char(64)->comment(''),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ];
    }

    /**
     * 应用版本表
     * @table `app_version`.
     */
    public function buildTableAppVersion() {
        return [
            'name' => 'app_version',
            'comment' => '应用版本表',
            'columns' => [
                'id' => $this->primaryKey(),
                'admin_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('发布人ID'),
                'client_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('发布人ID'),
                'os' => $this->char(32)->notNull()->comment('操作系统'),
                'build' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('build版本序列号'),
                'version' => $this->char(32)->notNull()->comment('版本号'),
                'sign' => $this->char(32)->comment('APK文件签名md5值'),
                'link' => $this->char(255)->comment('下载地址'),
                'explain' => $this->string(255)->comment('版本升级说明'),
                'force' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('是否强制升级'),
                'thumb' => $this->char(255)->comment('缩略图'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('版本发布状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ];
    }

    /**
     * 客户端设备信息表
     * @table `app_device`.
     */
    public function buildTableAppDevice() {
        return [
            'comment' => '客户端设备信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'os_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('操作系统'),
                'sn' => $this->char(64)->notNull()->comment('序列号'),
                'build' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('Build版本序列号'),
                'os' => $this->enum(['android', 'ios', 'windows', 'linux', 'unix'])->notNull()->defaultValue('android')->comment('操作系统'),
                'system' => $this->char(64)->comment('操作系统'),
                'model' => $this->string(64)->comment('机型'),
                'brand' => $this->char(64)->comment('品牌'),
                'platform' => $this->char(32)->comment('平台'),
                'imei' => $this->char(64)->comment('IMEI机器码'),
                'push_token' => $this->char(32)->comment('推送Token'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ];
    }

    /**
     * 接口访问授权令牌表
     * @table `app_token`.
     */
    public function buildTableAppToken() {
        return [
            'comment' => '接口访问授权令牌表',
            'columns' => [
                'id' => $this->primaryKey(),
                'client_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('客户端应用ID'),
                'device_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('设备ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'build' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'os' => $this->char(32)->defaultValue('')->comment('用户ID'),
                'access_token' => $this->char(64)->comment('AccessToken'),
                'scope' => $this->char(64)->defaultValue('')->comment('授权范围'),
                'expired_at' => $this->unixTimestamp()->comment('过期时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ];
    }

    /**
     * 接口访问日志表
     * @table `app_access_log`.
     */
    public function buildTableAppAccessLog() {
        return [
            'comment' => '接口访问日志表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户UID'),
                'device_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('设备ID'),
                'token_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('访问TokenID'),
                'ip' => $this->integer(10)->unsigned()->notNull()->defaultValue(0)->comment('IP'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ];
    }

}
